/*
 * 吐槽功能
 * 
 * */

package com.shanghaionstar.mro;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.onstar.cn.vehicle.entity.ErrorEnum;
import com.onstar.cn.vehicle.entity.ErrorInfo;
import com.onstar.cn.weight.TopLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.ErrorCodeUtil;
import com.shanghaionstar.utils.JoinMeConstants;
import com.shanghaionstar.utils.RequestUtil;
import com.vcyber.sdk.foro.TaskControllerForO;

public class MrOTrshTalkFragment extends MrOBaseFragment {
	private View view;
	@FindViewById
	private Button trash_talk;
	private MrOActivity mParent;
	@FindViewById
	private TopLayout top_layout;
	private TaskControllerForO tc = null;
	@FindViewById
	private TextView tv_talk;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (view == null) {
			view = inflater.inflate(R.layout.mro_trash_talk, container, false);
			injectView(view);
		} else {
			super.onResumeView(view);
		}

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mParent = (MrOActivity)getActivity();
		initview();
	}

	private void initview() {
		trash_talk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
//				隐藏输入键盘
				mParent.hideSoftInput();
				setTalk(tv_talk.getText().toString());
			}
		});
		/*
		 * top_layout.setLeftButtonClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) {
		 * CommonUtil.hideKeyBoard(getActivity(), trash_talk);
		 * mParent.mFragmentManager.popBackStack(); } });
		 */
	}

	// 吐槽 发送请求
	private void setTalk(final String value) {
		final Message msg = resultHandler.obtainMessage();
		RequestUtil.getInstance().getFeedback(value, new ResponseHandler() {

			@Override
			public void onSuccess(String tag, String json) {
				msg.what = JoinMeConstants.SUCCESS;
				tc.feedback();
				resultHandler.sendMessage(msg);
			}

			@Override
			public void onError(String tag, HttpException e) {
				if (e != null
						&& !ErrorEnum.SB024_MSG001.getStringValue().equals(e.getErrorCode())) {
					msg.what = JoinMeConstants.ERROR;
					msg.getData().putSerializable("errorInfo", e.getMessage());
				} else {
					msg.what = JoinMeConstants.FAILURE;
					ErrorInfo errorInfo = BusinessUtil.getErrorInfoByException(getActivity(), e);
					msg.getData().putSerializable("errorInfo", errorInfo);
				}
				resultHandler.sendMessage(msg);
			}
		}, getActivity(),true);
	}


	/**
	 * 结果监听
	 */
	private Handler resultHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case JoinMeConstants.FAILURE:
				ErrorInfo errorInfo = (ErrorInfo) msg.getData().getSerializable("errorInfo");
				ErrorCodeUtil.ShowErrorSompMsg(getActivity(), errorInfo);
				break;
			case JoinMeConstants.SUCCESS:
				AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning, R.string.feedback_talk,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mParent.fragmentManager.popBackStack();
							}
						});
				break;
			case JoinMeConstants.ERROR:
				ErrorInfo ErrorInfo = (com.onstar.cn.vehicle.entity.ErrorInfo) msg.getData().getSerializable("errorInfo");
				ErrorCodeUtil.ShowErrorSompMsg(getActivity(), ErrorInfo);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		view = null;
	}

}
